package org.arch_learn.spring_cloud.sca_homework.service_email_8082.service;

import org.apache.dubbo.config.annotation.Service;
import org.arch_learn.spring_cloud.sca_homework.service_common_dependency.dubbo_api.IEmailDubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;

@Service
public class EmailDubboService implements IEmailDubboService {
    @Value("${spring.mail.username}")
    private String mailSenderAccount;
    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public Boolean sendEmail(String email, String code) {
        Boolean result = true;
        try {
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setFrom(mailSenderAccount);
            simpleMailMessage.setText(code);
            simpleMailMessage.setSubject("请查收验证码");
            simpleMailMessage.setTo(email);
            javaMailSender.send(simpleMailMessage);
        } catch (Exception e) {
            result = false;
        }
        return result;
    }
}
